<?php

namespace App\V3\V3_2_7\Services;

use App\Services\CommonService;
use App\V3\V3_2_7\Models\CustomTagModel;
use App\V3\V3_2_7\Models\SmPhotoAlbumModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;

class SmPotoAlbumService
{
    /**
     *
     * Author: zlf
     * Created at 2024/8/14 17:03
     * @param $data
     * @return JsonResponse|null
     */
    public static function add($data): ?JsonResponse
    {
        $time = $data['time'];
        unset($data['time']);
        $id = SmPhotoAlbumModel::query()->insertGetId($data);
        if ($id) {
            CommonService::operateResult(
                $data['user_id'],
                $time,
                $data,
                '添加成功'
            );
            return success($id);
        } else
            return fail('', '发布失败');
    }

    public static function edit($data): ?JsonResponse
    {
        $bool = false;
        $time = $data['time'];
        $info = SmPhotoAlbumModel::query()
            ->where([
                'id' => $data['id'],
                'delete_flag' => 0,
                'main_id' => $data['main_id']
            ])->first();
        if (!empty($info)) {
            unset($data['id']);
            unset($data['time']);
            $bool = SmPhotoAlbumModel::query()
                ->where('id', $info['id'])->update($data);
        }
        if ($bool) {
            CommonService::operateResult(
                $data['user_id'],
                $time,
                $data,
                '编辑成功'
            );
            return success();
        } else
            return fail('', '编辑失败！');
    }

    public static function del($data): ?JsonResponse
    {
        if (empty($data['id'])) {
            return fail('', '相册主键ID，不能为空！');
        }
        $bool = false;
        $info = SmPhotoAlbumModel::query()
            ->where([
                'id' => $data['id'],
                'delete_flag' => 0,
                'main_id' => $data['main_id']
            ])->first();
        if (!empty($info)) {
            $info->delete_flag = 1;
            $bool = $info->save();
        }
        if ($bool) {
            CommonService::operateResult(
                $data['user_id'],
                $data['time'],
                $data,
                '删除成功'
            );
            return success();
        } else
            return fail('', '删除失败！');
    }

    public static function list($data): JsonResponse
    {
        $list = SmPhotoAlbumModel::query()
            ->where([
                'delete_flag' => 0,
                'main_id' => $data['main_id']
            ])->orderBy('created_at', 'desc')
            ->paginate($data['pageSize'], ['*'], '', $data['currentPage'])->toArray();
        if (!empty($list['data'])) {

            foreach ($list['data'] as &$val) {
                $val['logo'] = '/uploads/logo/img/2023-02-23/default-logo.png?x-oss-process=image/resize,m_fill,w_500,h_400';
                $val['label_name'] = CustomTagModel::query()
                    ->where('id', $val['label_id'])
                    ->value('label_name') ?? '';
                if (!empty($val['goods_img'])) {
                    $goodsImgArr = explode(',', $val['goods_img']);
                    if (!empty($goodsImgArr)) {
                        $val['logo'] = $goodsImgArr[0] . '?x-oss-process=image/resize,m_fill,w_500,h_400';
                    }
                    $val['goods_img'] = $goodsImgArr;
                }
                if (!empty($val['detail_img'])) {
                    $detailImgArr = explode(',', $val['detail_img']);
                    $val['detail_img'] = $detailImgArr;
                }
                if (!empty($val['title'])) {
                    $val['detail'] = $val['title'];
                    $val['title'] = mb_substr($val['title'], 0, 30, 'UTF-8');
                }
            }
        }
        return success($list);
    }

    public static function selAlbumGoodsImg($data): array
    {
        $ImgArr = [];
        $goodsImgArr = SmPhotoAlbumModel::query()
            ->where([
                'delete_flag' => 0,
                ['goods_img','!=',''],
                'main_id' => $data['main_id'],
            ])->pluck('goods_img')->toArray();
        if (!empty($goodsImgArr)) {
            $goodsImgArr = collect($goodsImgArr)->join(',');
            $goodsImgArr = explode(',',$goodsImgArr);
            $ImgArr = array_values(array_filter(array_unique($goodsImgArr)));
        }
        return $ImgArr;
    }
}
